import {defineStore} from 'pinia'
import {getUUID} from '@/libs/uuid_token'
import {reqAddOrUpdateShopCart, reqGoodsInfo} from '@/api'
import {GoodInfoType} from './types'
//detail模块的仓库
export const useDetailStore = defineStore('detail', {
    state: () => ({
        goodInfo: <GoodInfoType>{},
        //游客临时身份
        uuid_token: getUUID()
    }),
    getters: {
        //路径导航简化的数据
        categoryView(state) {
            return state.goodInfo.categoryView || {}
        },
        //简化产品信息的数据
        skuInfo(state) {
            return state.goodInfo.skuInfo || {}
        },
        //产品售卖属性的简化
        spuSaleAttrList(state) {
            return state.goodInfo.spuSaleAttrList || []
        }
    },
    actions: {
        //获取产品信息
        async getGoodInfo(skuId: string) {
            let result = await reqGoodsInfo(skuId)
            if (result.code == 200) {
                this.goodInfo = result.data
            }
        },
        //加入购物车 或 修改某一个产品的个数
        async addOrUpdateShopCart({skuId, skuNum}: {skuId: string; skuNum: string}) {
            let result = await reqAddOrUpdateShopCart(skuId, skuNum)
            if (result.code == 200) {
                return 'ok'
            } else {
                return Promise.reject(new Error('fail'))
            }
        }
    }
})
